package com.jelly.web.modules.system.role.service.impl;

import com.jelly.common.util.ListUtil;
import com.jelly.common.util.StringUtils;
import com.jelly.web.modules.system.role.dao.UserRoleMapper;
import com.jelly.web.modules.system.role.domain.UserRole;
import com.jelly.web.modules.system.role.domain.UserRoleExample;
import com.jelly.web.modules.system.role.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @创建人 ql
 * @创建时间 2018-09-01 10:39
 * @项目名称 jelly
 * @功能描述:
 **/
@Service
public class UserRoleServiceImpl implements IUserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 查询用户 角色
     * @param userId
     * @param roleId
     * @return
     */
    @Override
    public List<UserRole> getUserRole(String userId, String roleId) {
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        if(StringUtils.isNotBlank(roleId)){
            criteria.andRoleIdEqualTo(roleId);
        }
        return userRoleMapper.selectByExample(example);
    }

    /**
     * 新增用户 角色
     * @param userId
     * @param roleIdList
     * @return
     */
    @Override
    public void addUserRole(String userId, List<String> roleIdList) {

        if(StringUtils.isNotBlank(userId)){

            if(ListUtil.isNotList(roleIdList)){

                for (String roleId:roleIdList){

                    UserRole userRole = new UserRole();
                    userRole.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    userRole.setUserId(userId);
                    userRole.setRoleId(roleId);
                    userRoleMapper.insert(userRole);

                }

            }

        }

    }

    /**
     * 删除用户 角色 id查询关联关系
     * @param userId
     * @return
     */
    @Override
    public boolean delectUserRole(String userId) {
        UserRoleExample example = new UserRoleExample();
        example.createCriteria().andUserIdEqualTo(userId);
        return userRoleMapper.deleteByExample(example) > 0;
    }

    /**
     * 批量删除用户id关联关系
     * @param userId
     * @return
     */
    @Override
    public boolean delectsUserRole(List<String> userId) {
        UserRoleExample example = new UserRoleExample();
        example.createCriteria().andUserIdIn(userId);
        return userRoleMapper.deleteByExample(example) > 0;
    }
}
